package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class SearchHelpServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String res = "";
        resp.setContentType("text/plain");
        String keyword = req.getParameter("keyword");
        CatalogService catalogService = new CatalogService();
        List<Product> products = catalogService.searchProductList(keyword);
        //把得到的结果拼接成一个字符串
        for (int i = 0; i < products.size(); i++) {
            res+=products.get(i) + ",";
        }
        PrintWriter writer = resp.getWriter();
        writer.write(res);
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
